package com.astrob.activitys;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.TextView;

import com.astrob.NavFrameSDK.NavPosition;
import com.astrob.NavFrameSDK.POIDescription;
import com.astrob.activitys.PositionProvider.PosCallBack;
import com.astrob.model.FavoriteData;
import com.astrob.model.LonLat;
import com.astrob.model.Msg;
import com.astrob.naviframe.Start;
import com.astrob.nhcmap.R;
import com.astrob.util.Utils;
import com.astrob.view.Car;
import com.astrob.view.MapViewS;
import com.astrob.view.MapViewS.MapCallBack;
import com.astrob.view.NaviInfoView;
import com.astrob.view.TipInfoView;
import com.astrob.view.ZoomView;

public class NaviSActivity extends Activity implements MapCallBack, PosCallBack{

	// MapMode -- 移图模式/导航模式
	public final int MOVING_MODE = 0;
	public final int NAVI_MODE = 1;
	private int mMapMode = -1;

	Car mCar = new Car();

	MapViewS mMapViewS;
	FrameLayout mSimuCtrl;
	ZoomView mZoom;
	NaviInfoView mNaviInfoView;
	TipInfoView mTipInfo;
	ImageButton mLocal;
	TextView mScale;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_map_s);
		mMapViewS = (MapViewS) this.findViewById(R.id.id_mapview);
		mMapViewS.mCallback = this;

		mSimuCtrl = (FrameLayout) findViewById(R.id.navi_simuctrl);
		mNaviInfoView = (NaviInfoView) findViewById(R.id.id_naviinfo);
		mTipInfo = (TipInfoView) findViewById(R.id.id_tipinfoview);
		mZoom = (ZoomView) findViewById(R.id.id_zoomview);
		mScale = (TextView) findViewById(R.id.id_map_scalebar);
		mLocal = (ImageButton) findViewById(R.id.btnlocal);

		PositionProvider.getInstance().init(this, this);

		double lon = 121.476455;
		double lat = 31.231706;
		mCar.navpos_.pos.lon = lon;
		mCar.navpos_.pos.lat = lat;
		Start.getInstance().MapKit().setMapCenter(new LonLat(lon, lat));
		Start.getInstance().MapKit().setScale(200000);

		initWnd(MOVING_MODE);
	}

	private void initWnd(int mapMode) {
		mMapMode = mapMode;

		if (MOVING_MODE == mapMode) {
			mSimuCtrl.setVisibility(View.GONE);
			mNaviInfoView.setVisibility(View.GONE);
			mTipInfo.setVisibility(View.GONE);
			Start.getInstance().MapKit().setRotation(0);
			// 回2D模式
			Start.getInstance().MapKit().setMapViewMode(0);
			mCar.setFreeMoving(true);
		} else if (NAVI_MODE == mapMode) {
			mCar.setFreeMoving(false);
			mNaviInfoView.setVisibility(View.VISIBLE);
			mTipInfo.setVisibility(View.GONE);
			Start.getInstance().MapKit().setScale(100000);
			mZoom.updateState(0);

			Start.getInstance().MapKit().setRotation(mCar.navpos_.dir);
			Start.getInstance().MapKit()
					.setPosition(mCar.getCarPos(), mCar.getCarLL());

		}
		mMapViewS.updateMap();
	}

	@SuppressLint("HandlerLeak")
	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
				case Msg.UPDATEPOS:
					matchPos();
					break;
				case Msg.SHOWTIP:
					break;
				case Msg.UPDATEMAP:
//					updateMapView();
					break;
				case Msg.FINISHUPDATEMAP:
//					finishUpdatedMapView();
					break;
			}
		}
	};

	int _auto2navimodeTime = 0;// 有GPS情况下15s无操作，回现在地

	@Override
	public void updatePos(NavPosition navpos, boolean isNetPos) {
		_isNetPos = isNetPos;
		if (mMapViewS._mapWidth != 0) {
			mCar.setMapsize(mMapViewS._mapWidth, mMapViewS._mapHeight);
		}
		mCar.navpos_ = navpos;

		mHandler.sendEmptyMessage(Msg.UPDATEPOS);
	}

	boolean _isNetPos = false;

	void matchPos() {
		_auto2navimodeTime++;
		if (MOVING_MODE == mMapMode) {
			if (_auto2navimodeTime > 15) {
				initWnd(NAVI_MODE);
			}
			if (_isNetPos) {
				Start.getInstance().MapKit().setMapCenter(mCar.getCarLL());
				mMapViewS.updateMap();
			}
			return;
		}

		if (mCar.getCarposMode() == Car.BOTTOMCENTER) {
			Start.getInstance().MapKit()
					.setPosition(mCar.getCarPos(), mCar.getCarLL());
			Start.getInstance().MapKit().setRotation(mCar.navpos_.dir);
		} else {
			Start.getInstance().MapKit().setMapCenter(mCar.getCarLL());
		}

		mMapViewS.updateMap();
	}

	public void onLocal(View v) {
		if (Utils.isFastDoubleClick())
			return;
		initWnd(NAVI_MODE);
	}

	@Override
	protected void onResume() {
		super.onResume();
		PositionProvider.getInstance().mCallback = this;
	}

	@Override
	protected void onPause() {
		super.onPause();
		PositionProvider.getInstance().mCallback = null;
		mMapViewS.recycleMap();
	}

	public void onZoomIn(View v) {
		Start.getInstance().MapKit().onZoomIn();
		if (mMapMode == NAVI_MODE) {
			Start.getInstance().MapKit()
					.setPosition(mCar.getCarPos(), mCar.getCarLL());
		}
		mMapViewS.updateMap();
	}

	public void onZoomOut(View v) {
		Start.getInstance().MapKit().onZoomOut();
		if (mMapMode == NAVI_MODE) {
			Start.getInstance().MapKit()
					.setPosition(mCar.getCarPos(), mCar.getCarLL());
		}
		mMapViewS.updateMap();
	}

	public void onSearcharound(View v) {
		if (Utils.isFastDoubleClick())
			return;

		Intent intent = new Intent(this, SearchAroundActivity.class);
		startActivityForResult(intent, 0);
	}

	public void onTipname(View v) {
		if (Utils.isFastDoubleClick())
			return;
	}

	public void onTipman(View v) {
		if (Utils.isFastDoubleClick())
			return;
	}

	public void onTipcar(View v) {
		if (Utils.isFastDoubleClick())
			return;

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
			new AlertDialog.Builder(this)
					.setTitle("是否退出地图？")
					.setPositiveButton("是",
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									finish();
									System.exit(0);
								}
							}).setNegativeButton("否", null).show();
			return true;
		}

		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void drawOverlay(Canvas canvas, float ws) {
		mCar.draw(this, canvas, ws, ws);

		//显示标签
		int count = Start.getInstance().mFavList.size();
		int resId;
		for (int i = 0; i < count; ++i) {
			FavoriteData fav = Start.getInstance().mFavList.get(i);
			LonLat poiLL = fav.getLl();
			if (!mMapViewS.isInMapRegion(poiLL)) {
				continue;
			}
			resId = R.drawable.marker01 + i;
			if (resId > R.drawable.marker15) {
				resId = R.drawable.marker15;
			}

			Bitmap bm = ((BitmapDrawable) getResources().getDrawable(resId))
					.getBitmap();
			Point e = Start.getInstance().MapKit().LL2XY(poiLL);
			int x = (int) (e.x * ws - bm.getWidth() / 2);
			int y = (int) (e.y * ws - bm.getHeight());
			canvas.drawBitmap(bm, x, y, null);
		}

		//画完图
		mZoom.updateState();
		mScale.setText(Start.getInstance().MapKit().getScaleString());
	}

	@Override
	public void popTipInfo(POIDescription data) {
		Log.d("astrob2", "popTipInfo");
		mTipInfo.showBtn();
		mTipInfo.setText(data.name);
	}

	@Override
	public void moveMap(boolean removeTip) {
		if (removeTip) {
			mTipInfo.setVisibility(View.GONE);
		}

		_auto2navimodeTime = 0;
		if (mMapMode == MOVING_MODE) {
			return;
		}
		initWnd(MOVING_MODE);
	}

	@Override
	public void touchUp() {

	}

	@Override
	public void longPress(LonLat ll) {
		mTipInfo.setVisibility(View.GONE);
		FavoriteData fav = new FavoriteData();
		fav.setLl(ll);
		Start.getInstance().mFavList.add(fav);
		mMapViewS.updateMap();
	}

	@Override
	public void doubleClick(LonLat ll) {

	}

	@Override
	public void singleClick(LonLat ll, Point tipPoint) {

	}

}
